package DAL;

import Entities.TblClass;
import DatabaseAccess.SQLHelper;
import java.sql.ResultSet;
import java.util.*;

public class ClassDAL {

    private ResultSet objResult;
    public List<TblClass> list;

    public ClassDAL() {
    }

    public boolean add(TblClass classObj) {
        int result = 0;
        String[] params = new String[4];
        params[0] = classObj.getClassName();
        params[1] = String.valueOf(classObj.getSemesterId());
        params[2] = String.valueOf(classObj.getTeacherId());
        params[3] = String.valueOf(classObj.getBranchId());

        try {
            result = SQLHelper.executeUpdate("ProcInsertClass", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean update(TblClass classObj) {
        int result = 0;
        String[] params = new String[5];
        params[0] = String.valueOf(classObj.getClassId());
        params[1] = classObj.getClassName();
        params[2] = String.valueOf(classObj.getSemesterId());
        params[3] = String.valueOf(classObj.getTeacherId());
        params[4] = String.valueOf(classObj.getBranchId());
        try {
            result = SQLHelper.executeUpdate("procUpdateClass", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean delete(TblClass classObj){
        int result = 0;
        String[] params = new String[1];
        params[0] = String.valueOf(classObj.getClassId());
        try {
            result = SQLHelper.executeUpdate("procDeleteClass", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public List<TblClass> getAll() {
        list = new ArrayList<TblClass>();
        try {
            objResult = SQLHelper.executeQuery("procGetAllClass", null);
            while (objResult.next()) {
                TblClass classObj = new TblClass();
                classObj.setClassId(objResult.getInt("ClassId"));
                classObj.setClassName(objResult.getNString("ClassName"));
                classObj.setSemesterId(objResult.getInt("SemesterId"));
                classObj.setTeacherId(objResult.getInt("TeacherId"));
                classObj.setBranchId(objResult.getInt("BranchId"));
                classObj.setStartDate(objResult.getDate("StartDate"));
                classObj.setIsDelete(objResult.getBoolean("IsDelete"));
                list.add(classObj);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public TblClass getById(int id) {
        TblClass classObj = new TblClass();
        String[] params = new String[1];
        params[0] = String.valueOf(id);
        try {
            objResult = SQLHelper.executeQuery("procGetClassById", params);
            while (objResult.next()) {
                classObj.setClassId(objResult.getInt("ClassId"));
                classObj.setClassName(objResult.getNString("ClassName"));
                classObj.setSemesterId(objResult.getInt("SemesterId"));
                classObj.setTeacherId(objResult.getInt("TeacherId"));
                classObj.setBranchId(objResult.getInt("BranchId"));
                classObj.setStartDate(objResult.getDate("StartDate"));
                classObj.setIsDelete(objResult.getBoolean("IsDelete"));
            }

            return classObj;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public List<TblClass> getAllByBranchId(int branchId) {
        list = new ArrayList<TblClass>();
        String[] params = new String[1];
        params[0] = String.valueOf(branchId);
        try {
            objResult = SQLHelper.executeQuery("procGetAllClassByBranchId", params);
            while (objResult.next()) {
                TblClass classObj = new TblClass();
                classObj.setClassId(objResult.getInt("ClassId"));
                classObj.setClassName(objResult.getNString("ClassName"));
                classObj.setSemesterId(objResult.getInt("SemesterId"));
                classObj.setTeacherId(objResult.getInt("TeacherId"));
                classObj.setBranchId(objResult.getInt("BranchId"));
                classObj.setStartDate(objResult.getDate("StartDate"));
                classObj.setIsDelete(objResult.getBoolean("IsDelete"));
                list.add(classObj);
            }
            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }


    public List<TblClass> getBySemesterId(int semesterId) {
        list = new ArrayList<TblClass>();
        String[] params = new String[1];
        params[0] = String.valueOf(semesterId);
        try {
            objResult = SQLHelper.executeQuery("procGetClassBySemesterId", params);
            while (objResult.next()) {
                TblClass classObj = new TblClass();
                classObj.setClassId(objResult.getInt("ClassId"));
                classObj.setClassName(objResult.getNString("ClassName"));
                classObj.setSemesterId(objResult.getInt("SemesterId"));
                classObj.setTeacherId(objResult.getInt("TeacherId"));
                classObj.setBranchId(objResult.getInt("BranchId"));
                classObj.setStartDate(objResult.getDate("StartDate"));
                classObj.setIsDelete(objResult.getBoolean("IsDelete"));
                list.add(classObj);
            }
            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

}
